package com.dengpp.pdform.holder;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.dengpp.pdform.R;
import com.dengpp.pdform.adapter.AttributeAdapter;
import com.dengpp.pdform.base.BaseAttributeViewHolder;
import com.dengpp.pdform.config.AttributeConfig;

import java.util.List;

public class DropdownViewHolder extends BaseAttributeViewHolder {
    private TextView tvTitle;
    private Spinner spinner;
    private TextView tvValue;
    private AttributeConfig currentAttribute;

    public DropdownViewHolder(@NonNull View itemView) {
        super(itemView);
        tvTitle = itemView.findViewById(R.id.tv_title);
        spinner = itemView.findViewById(R.id.spinner);
        tvValue = itemView.findViewById(R.id.tv_value);
    }

    @Override
    public void bind(AttributeConfig attribute, boolean isEditMode, AttributeAdapter.OnAttributeChangeListener listener) {

        // 保存当前绑定的属性
        this.currentAttribute = attribute;

        // 移除旧的监听器
        spinner.setOnItemSelectedListener(null);

        // 使用带星号的标题
        tvTitle.setText(getTitleWithRequiredStar(attribute.getTitle(), attribute.isRequired()));

        List<String> options = attribute.getOptions();
        if (options == null || options.isEmpty()) {
            return;
        }

        if (isEditMode && attribute.isEditable()) {
            spinner.setVisibility(View.VISIBLE);
            tvValue.setVisibility(View.GONE);

            ArrayAdapter<String> adapter = new ArrayAdapter<>(
                    itemView.getContext(),
                    android.R.layout.simple_spinner_item,
                    options
            );
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);

            // 设置选中项
            String currentValue = attribute.getValue() != null ? attribute.getValue().toString() : "";
            int position = options.indexOf(currentValue);
            if (position >= 0) {
                spinner.setSelection(position);
            }

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (listener != null && position >= 0) {
                        listener.onAttributeChanged(attribute.getKey(), options.get(position));
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {}
            });

        } else {
            spinner.setVisibility(View.GONE);
            tvValue.setVisibility(View.VISIBLE);
            tvValue.setText(attribute.getValue() != null ? attribute.getValue().toString() : "");
        }
    }
    /**
     * 在 ViewHolder 被回收时清理资源
     */
    @Override
    public void onViewRecycled() {
        super.onViewRecycled();
        spinner.setOnItemSelectedListener(null);
        currentAttribute = null;
    }
}
